package com.scoremg.service.impl;


import java.util.Date;
import java.util.List;

import com.scoremg.entity.po.CourseInfo;

import com.scoremg.entity.po.CourseSelectInfo;
import com.scoremg.entity.po.UserInfo;
import com.scoremg.entity.query.*;

import com.scoremg.entity.vo.PaginationResultVO;

import com.scoremg.exception.BusinessException;
import com.scoremg.mappers.CourseSelectInfoMapper;
import com.scoremg.mappers.UserInfoMapper;
import com.scoremg.service.CourseInfoService;

import com.scoremg.service.UserInfoService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import com.scoremg.mappers.CourseInfoMapper;

import com.scoremg.entity.enums.PageSize;
import org.springframework.transaction.annotation.Transactional;


@Service("courseInfoService")
public class CourseInfoServiceImpl implements CourseInfoService{

	@Resource
	private CourseInfoMapper<CourseInfo,CourseInfoQuery> courseInfoMapper;

	@Resource
	private UserInfoMapper<UserInfo, UserInfoQuery> userInfoMapper;


	@Resource
	private CourseSelectInfoMapper<CourseSelectInfo, CourseSelectInfoQuery> courseSelectInfoMapper;


	/**
	 * 根据条件查询列表
	 */
	public List<CourseInfo> findListByParam(CourseInfoQuery query) { 
		return this.courseInfoMapper.selectList(query);
	}

	/**
	 * 根据条件查询数量
	 */
	public Integer findCountByParam(CourseInfoQuery query){
		return this.courseInfoMapper.selectCount(query);
	}

	/**
	 * 分页查询
	 */
	public PaginationResultVO<CourseInfo> findListByPage(CourseInfoQuery  query) { 
		int count = this.findCountByParam(query);
		int pageSize = query.getPageSize()==null?PageSize.SIZE15.getSize():query.getPageSize();
		SimplePage page = new SimplePage(query.getPageNo(),count,pageSize);
		query.setSimplePage(page);
		List<CourseInfo> list = this.findListByParam(query);
		return new PaginationResultVO(count,page.getPageSize(),page.getPageNo(),page.getPageTotal(),list);
	}

	/**
	 * 新增
	 */
	public Integer add(CourseInfo bean) { 
		return this.courseInfoMapper.insert(bean);
	}

	/**
	 * 批量新增
	 */
	public Integer addBatch(List<CourseInfo> listBean) { 
		if (listBean == null || listBean.isEmpty()) {
			return 0;
		}
		return this.courseInfoMapper.insertBatch(listBean);
	}

	/**
	 * 批量新增或者修改
	 */
	public Integer addOrUpdateBatch(List<CourseInfo> listBean) { 
		if (listBean == null || listBean.isEmpty()) {
			return 0;
		}
		return this.courseInfoMapper.insertOrUpdateBatch(listBean);
	}

	/**
	 * 根据CourseId查询
	 */
	public CourseInfo getCourseInfoByCourseId(Integer courseId) { 
		return this.courseInfoMapper.selectByCourseId(courseId);
	}

	/**
	 * 根据CourseId更新
	 */
	public Integer updateCourseInfoByCourseId(CourseInfo bean, Integer courseId) { 
		return this.courseInfoMapper.updateByCourseId(bean,courseId);
	}

	/**
	 * 根据CourseId删除 课程可以删除 ：1.
	 */
	@Transactional(rollbackFor = Exception.class)
	public Integer deleteCourseInfoByCourseId(Integer courseId) {


		// 学生选课表
		CourseSelectInfoQuery courseSelectInfoQuery = new CourseSelectInfoQuery();
		courseSelectInfoQuery.setCourseId(courseId);
		Integer count1 = courseSelectInfoMapper.selectCount(courseSelectInfoQuery);
		if (count1 >=1) {
			throw new BusinessException("该课程下学生信息选课信息不为空，无法删除");
		}
		return this.courseInfoMapper.deleteByCourseId(courseId);
	}


	/**
	 * 保存课程信息
	 * @param courseInfo 课程信息实体
	 */
	@Override
	public void saveCourse(CourseInfo courseInfo) {
		UserInfo userInfo = userInfoMapper.selectByStuJobNo(courseInfo.getTeacherId());
		if (userInfo == null) {
			throw new BusinessException("教师信息不存在,请检查教师学工号");
		}

		if (!userInfo.getName().equals(courseInfo.getTeacherName())) {
			throw new BusinessException("教师信息不一致，请检查后重试");
		}

		if (courseInfo.getCourseId() == null) {  // 课程新增
				courseInfoMapper.insert(courseInfo);
		}else {
			courseInfoMapper.updateByCourseId(courseInfo,courseInfo.getCourseId());
		}
	}
}